﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Bids;
using BLL.Bids.Impl;
using Model;
using GenericLibrariesToolKit;
using Utils;
using BLL.Privilege;
using BLL.Privilege.Impl;
using System.Text;

public partial class Page_Bids_BidsList : System.Web.UI.Page
{
    BidBO bidBO = new BidBOImpl();
    protected void Page_Load(object sender, EventArgs e)
    {   
     
            txtPostBidOpenningDate.Text = DateTimeUtil.GetDayDateTime();
            bidsPrivilegeControl();
            BidDataBind();
    }
    //标绑定数据
    private void BidDataBind()
    {
        List<Bid> listBids = new List<Bid>();
        string type = Request["type"].ToString();
        if (type == "myBid")
        {
            listBids = bidBO.GetEntityList("EnterpriseId", WebUtil.GetIdentifier(Session["userId"]), "StartDate");

        }
        else if (type == "allBid")
        {
            QueryResult<Bid> queryBids = bidBO.GetScrollData();
            listBids = queryBids.ResultList;
        }
        else if (type == "myReplyBid")
        {
            listBids = bidBO.GetEntityList("ExpertId", WebUtil.GetIdentifier(Session["userId"]), "StartDate");

        }
        GVBid.DataSource = listBids;
        GVBid.DataBind();
    }
    //发标事件
    protected void btnPostBidSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string bidTitle = txtPostBidTitle.Text.Trim();
        string bidIndustry = ddltxtPostBidIndustry.Text;
        string bidContact = txtPostBidContact.Text.Trim();
        string bidAddress = txtPostBidAddress.Text.Trim();
        string bidPhone = txtPostBidPhone.Text.Trim();
        string Fax = txtPostBidFax.Text.Trim();
        string email = txtPostBidEmail.Text.Trim();
        string openDate = txtPostBidOpenningDate.Text.Trim();
        string expirationDate = txtPostBidExpirationDate.Text.Trim();
        string bidDetails = txtPostBidDetails.Text.Trim();
        Bid bid = new Bid()
        {
            BidAddress = bidAddress,
            BidDetail = bidDetails,
            BidEmail = email,
            BidFax = Fax,
            BidIndustry = bidIndustry,
            BidPhone = bidPhone,
            BidTitle = bidTitle,
            EnterpriseId = WebUtil.GetIdentifier(Session["userId"]),
            ContactType = bidContact,
            StartDate = openDate,
            EndDate = expirationDate,
        };
        bool result = bidBO.Add(bid);
        if (result)
        {
            Response.Write("<script language=javascript>alert('success！')</script>");
            Response.Write("<script language=javascript>window.location.href='BidsList.aspx?type=myBid'</script>");

        }
        else
        {
            Response.Write("<script>alert('Post Fail')</script>");
        }

    }
    //点击某个标跳转到该标的详细信息页面
    protected void LBTitle_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string bidId = bt.CommandArgument.ToString();
        Response.Redirect("BidDetails.aspx?bidId=" + bidId);
    }

    /// <summary>
    ///对发標进行权限控制
    /// </summary>
    public void bidsPrivilegeControl()
    {
        //获取用户ID
        string userID = WebUtil.GetIdentifier(Session["userId"]);
        RoleRightBO roleRightBO = new RoleRightBOImpl();
        //如果用户为游客，返回false
        if (userID == null)
            return;
        Dictionary<string, bool> privilege = new Dictionary<string, bool>();
        privilege.Add("postBids", roleRightBO.IsPrivilege(userID, "postBids", OperateType.add));
        privilege.Add("bidsReply", roleRightBO.IsPrivilege(userID, "bidsReply", OperateType.add));
        postBidsUIControl(privilege);
    }

    /// <summary>
    ///对显示进行控制
    /// </summary>
    public void postBidsUIControl(Dictionary<string, bool> privilege)
    {
        string type = Request["type"].ToString();
        if (privilege["postBids"])
        {
            if (type == "callBid")
            {
                StringBuilder script = new StringBuilder();
                script.Append("<script>");
                script.Append("document.getElementById('BidsList').style.display='none';");
                script.Append("</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
            }
            else if (type == "myBid")
            {
                StringBuilder script = new StringBuilder();
                script.Append("<script>");
                script.Append("document.getElementById('CallForABid').style.display='none';");
                script.Append("</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
            }
            else
            {
                StringBuilder script = new StringBuilder();
                script.Append("<script>");
                script.Append("document.getElementById('CallForABid').style.display='none';");
                script.Append("</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
            }
        }
        else if (privilege["bidsReply"])
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('CallForABid').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());

        }
    }
    //返回事件
    protected void btnBidsBack_Click(object sender, ImageClickEventArgs e)
    {

        Response.Write("<script language=javascript>history.go(-2);</script>");

        
    }
    protected void GVBid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVBid.PageIndex = e.NewPageIndex;
        this.GVBid.DataBind();
    }
}
